import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios';
import createPersistedState from "vuex-persistedstate";
import logger from 'vuex/dist/logger'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    AllList: [],
    AllInpCheck :false
  },
  mutations: {
    renderList(state,payload){
      if(!state.AllList.length){
        state.AllList = payload
      }
      
    },
    // 编辑
    onSubmit(state,payload){
      
      state.AllList.forEach((item,index)=>{
        if(item.id == payload.id){
          Object.assign(item,payload)
        }
      })
      // state.AllList = obj
    },
    // 删除
    ClearBtn(state,payload){
      state.AllList.forEach((item,index)=>{
        if(item.id == payload){
          state.AllList.splice(index,1);
        }
      })
    },
    // 新增
    newAdd(state,payload){
      state.AllList.push(payload);
    },
    // 单选
    InpBtn(state,payload){
      state.AllList.forEach((item,index)=>{
        if(item.id == payload){
          item.flag = !item.flag;
        }
      })
      state.AllInpCheck = state.AllList.every(item=>item.flag);
      
    },
    AllInpBtn(state,payload){
      state.AllInpCheck = payload;
      state.AllList.forEach((item,index)=>{
        item.flag = payload;
      })
    }
  },
  actions: {
    asyncList(context,payload){
      axios.get('/api/list').then(res=>{
          this.commit('renderList',res.data)
      })
    }
    
  },
  modules: {
  },
  plugins: [logger(),createPersistedState({
    storage: window.sessionStorage
  })],
  // plugins: [logger(),createPersistedState()],
})
